
function clickEvenet(ev)
{
	console.log( ev);}

var ganttGraphics;
// Create jsColor object
var col = new jsColor("green");
// Create jsPen object
var pen = new jsPen(col, 1);
var taskDoubleClickEvent;
var onLoadGanttDivPos = findPos(document.getElementById("ganttDiv"));;
var oldGanttDivPos ;
function mouseMove(evt) {
	ev = evt || window.event;
	var dEl0 = window.document.documentElement, dEl1 = window.document.body, corrector = new Array((dEl0.scrollLeft||dEl1.scrollLeft),(dEl0.scrollTop||dEl1.scrollTop));
	console.log("Mouse X:" + ev.pageX + " Mouse Y:" + ev.pageY);
	console.log(taskDoubleClickEvent);
	ganttGraphics.clear();
	var ganttDivPos = findPos(document.getElementById("ganttDiv"));
	if(ganttDivPos[0] != oldGanttDivPos[0] || ganttDivPos[1] != oldGanttDivPos[1])
		{
			clearLine();
		}
	console.log(ganttDivPos);

	var px = new jsPoint(taskDoubleClickEvent.clientX - ganttDivPos[0] + corrector[0],
			taskDoubleClickEvent.clientY - ganttDivPos[1]+ corrector[1] );
	var py = new jsPoint(ev.pageX - ganttDivPos[0], ev.pageY - ganttDivPos[1]);
	console.log(px);
	ganttGraphics.drawLine(pen, px, py);

}

function clearLine() {
	console.log('clearLine');
	document.getElementById("ganttDiv").removeEventListener("mousemove",
			mouseMove, false);
	document.getElementById("ganttDiv").removeEventListener("mouseup",
			clearLine, false);
	ganttGraphics.clear();

	console.log("clicked ");
}

function setTask(task,ganttProject)
{
	/*var task = schedule[field];
	console.log(task);
	var ganttTask = new GanttTaskInfo(task.id, task.name,
			task.startDate, task.duration,
			task.percentageComplete, task.preTaskId);
	if(task.childTaskSize > 0)
		{
		   setChildTasks(ganttTask,task);
		}*/
	var ganttTask = new GanttTaskInfo(task.id, task.name,
			task.startDate, task.duration,
			task.percentageComplete, task.preTaskId);
	//ganttProject.addTask(ganttTask);
	for ( var field in task) {
		if (field.substring(0, 4) == 'task') {
			var childTask = task[field];
			console.log(childTask);
			ganttTask.addChildTask(setTask(childTask,ganttProject));
			
		}
		
	}
		return ganttTask;
}


var taskClickTime = 0;
function createChartControl(htmlDiv1) {
	
	console.log('schedule:start');
	/*ScheduleRemoteBean.test(function (aaa){
		console.log(aaa);
	});
	ScheduleRemoteBean.getProjectsCount(function (aaa){
		var i = 0;
		for (i; i < aaa; i++) {
			ScheduleRemoteBean.getProject(i, function(ppp) {
				console.log('get Projects output');
				console.log(ppp);
				console.log('get Projects output');
			});
		}
	});*/
	
	// Initialize Gantt data structures
	// project 1
	var project1 = new GanttProjectInfo(1, "IPMSS", new Date(2011, 11,
			30));
	/*
	 * var parentTask1 = new GanttTaskInfo(1, "Old code review", new Date(2011,
	 * 5, 11), 208, 50, ""); parentTask1.addChildTask(new GanttTaskInfo(2,
	 * "Convert to J#", new Date(2011, 5, 11), 100, 40, ""));
	 * parentTask1.addChildTask(new GanttTaskInfo(13, "Add new functions", new
	 * Date(2011, 5, 12), 80, 90, ""));
	 */
	var parentTask1 = new GanttTaskInfo(1, "Analiza wymagań", new Date(2011, 11,
			30), 60, 100, "");
	project1.addTask(parentTask1);
	var parentTask2 = new GanttTaskInfo(2, "Architektura aplikacji", new Date(2011, 12,
			11), 177, 85, "1");
	project1.addTask(parentTask2);
	parentTask2.addChildTask(new GanttTaskInfo(21, "Projekt szkieletu aplikacji", new Date(2011, 12,
			11), 33, 80, ""));
	parentTask2.addChildTask(new GanttTaskInfo(22, "Implementacja szkieletu", new Date(2011, 12,
			17), 60, 90, "21"));
	parentTask2.addChildTask(new GanttTaskInfo(23, "Definicja modulów", new Date(2011, 12,
			17), 20, 79, "21"));
	
	

	var parentTask3 = new GanttTaskInfo(3, "Harmonogramowanie(wykres Gantta)", new Date(2011, 12,
			21), 277, 60, "23");
	project1.addTask(parentTask3);
	parentTask3.addChildTask(new GanttTaskInfo(31, "Projekt modelu", new Date(2011, 12,
			21), 28, 100, ""));
	parentTask3.addChildTask(new GanttTaskInfo(32, "Implementacja modelu", new Date(2011, 12,
			25), 60,80, "31"));
	parentTask3.addChildTask(new GanttTaskInfo(33, "Testy modelu", new Date(2012,1,
			4), 20, 80, "32"));
	parentTask3.addChildTask(new GanttTaskInfo(34, "Projekt widoku", new Date(2011, 12,
			25), 23, 90, "31"));
	parentTask3.addChildTask(new GanttTaskInfo(35, "Implementacja widoku", new Date(2011, 12,
			29), 60, 60, "34"));
	parentTask3.addChildTask(new GanttTaskInfo(36, "Testy widoku", new Date(2012,1,
			12), 20, 60, "35"));
	
	
	var parentTask4 = new GanttTaskInfo(4, "Wirtualny pulpit", new Date(2011, 12,
			21), 277, 92, "23");
	project1.addTask(parentTask4);
	parentTask4.addChildTask(new GanttTaskInfo(41, "Projekt", new Date(2011, 12,
			21), 33, 100, ""));
	parentTask4.addChildTask(new GanttTaskInfo(42, "Implementacja", new Date(2011, 12,
			26), 90, 100, "41"));
	parentTask4.addChildTask(new GanttTaskInfo(43, "Testy", new Date(2012,1,
			4), 50, 79, ""));
	
	
	var parentTask5 = new GanttTaskInfo(5, "Moduły CRUD", new Date(2011, 12,
			21), 277, 92, "23");
	project1.addTask(parentTask5);
	var parentTask51 = new GanttTaskInfo(51, "Zarządzanie projektami", new Date(2011, 12,
			21), 260, 92, "");
	parentTask5.addChildTask(parentTask51);


	parentTask51.addChildTask(new GanttTaskInfo(52, "Projekt", new Date(2011, 12,
			21), 33, 70, ""));

	parentTask51.addChildTask(new GanttTaskInfo(53, "Implementacja", new Date(2011, 12,
			26), 90, 70, ""));
	parentTask51.addChildTask(new GanttTaskInfo(54, "Testy", new Date(2012,1,
			7), 50, 40, ""));
	
	var parentTask55 = new GanttTaskInfo(55, "Zarządzanie kontenerami widoku", new Date(2011, 12,
			21), 260, 92, "");
	parentTask5.addChildTask(parentTask55);


	parentTask55.addChildTask(new GanttTaskInfo(56, "Projekt", new Date(2011, 12,
			21), 33, 70, ""));

	parentTask55.addChildTask(new GanttTaskInfo(57, "Implementacja", new Date(2011, 12,
			26), 90, 70, ""));
	parentTask55.addChildTask(new GanttTaskInfo(58, "Testy", new Date(2012,1,
			7), 50, 40, ""));
	
	
	
	
	/*
	 * var project2 = new GanttProjectInfo(2, "Web Design", new Date(2011, 5,
	 * 17)); var parentTask22 = new GanttTaskInfo(62, "Fill HTML pages", new
	 * Date(2011, 5, 17), 157, 50, ""); parentTask22.addChildTask(new
	 * GanttTaskInfo(63, "Cut images", new Date(2011, 5, 22), 78, 40, ""));
	 * parentTask22.addChildTask(new GanttTaskInfo(64, "Manage CSS", null, 90,
	 * 90, "")); project2.addTask(parentTask22); var parentTask70 = new
	 * GanttTaskInfo(70, "PHP coding", new Date(2011, 5, 18), 120, 10, "");
	 * parentTask70.addChildTask(new GanttTaskInfo(71, "Purchase D control", new
	 * Date(2011, 5, 18), 50, 0, "")); project2.addTask(parentTask70);
	 */
	// Create Gantt control
	var ganttChartControl = new GanttChart();
	// Setup paths and behavior
	/* ganttChartControl.setImagePath("../../codebase/imgs/"); */
	ganttChartControl.setEditable(true);
	ganttChartControl.showTreePanel(true);
	ganttChartControl.showContextMenu(true);
	ganttChartControl.showDescTask(true, 'd,s-f');
	ganttChartControl.showDescProject(true, 'n,d');
	//ganttChartControl.showTooltip(true);
	// ganttChartControl.showContextMenu(true);
	// Load data structure
	ganttChartControl.addProject(project1);
	document.getElementById("ganttDiv").addEventListener("mousedown",clickEvenet,false);
	ganttChartControl.attachEvent("onTaskClick", function(task, event) {
		console.log(event);
		console.log("Task with Id = " + task.getId() + " is clicked.");
		var clickTime = (new Date()).getTime();
		console.log(clickTime);
		if (clickTime - taskClickTime < 400) {
			console.log("double click");
			taskDoubleClickEvent = event;
			oldGanttDivPos = findPos(document.getElementById("ganttDiv"));
			if (document.getElementById("ganttDiv").addEventListener) {
				document.getElementById("ganttDiv").addEventListener("mousemove",
						mouseMove, false);
				document.getElementById("ganttDiv").addEventListener("mouseup",
						clearLine, false);
		    }
		    else if (document.getElementById("ganttDiv").attachEvent) {
		    	console.log('attach');
		    	document.getElementById("ganttDiv").attachEvent("onmousemove",
						mouseMove, false);
				document.getElementById("ganttDiv").attachEvent("onmouseup",
						clearLine, false);
		    }
			

		}
		taskClickTime = clickTime;
	});
	ganttChartControl.attachEvent("onTaskEndResize", function(task) {
		/*
		 * if (task.getId()==13) {
		 * task.setPercentCompleted(Math.min(100,parseInt(100 *
		 * task._initialCompleteHours/task.getDuration()))) ; } else
		 */
		console.log("New duration = " + task.getDuration());

	});
	
	
	
	ScheduleRemoteBean.getProjects(function(ppp) {
		console.log('get Projects output');
		console.log(ppp);
		var count = ppp.projectsCount;
		for ( var i = 0; i < count; i++) {
			var project = getProjectById(ppp, i);

			console.log(project.id);
			console.log(project.name);
			var schedule = project.schedule;
			var ganttProject = new GanttProjectInfo(project.id, project.name,
					schedule.startDate);
			console.log(schedule['task0']);
			for ( var field in project.schedule) {
				if (field.substring(0, 4) == 'task') {
					var task = schedule[field];
					var ganttTask  = setTask(task,ganttProject);
					console.log(task);
				/*	var ganttTask = new GanttTaskInfo(task.id, task.name,
							task.startDate, task.duration,
							task.percentageComplete, task.preTaskId);
					if(task.childTaskSize > 0)
						{
						   setChildTasks(ganttTask,task);
						}
						*/
				//	console.log(ganttTask);
					//console.log(ganttProject);
					ganttProject.addTask(ganttTask);
				}
			}
		//	ganttChartControl.addProject(ganttProject);
		}
		console.log('get Projects output');
		


		// ganttChartControl.addProject(project2);
		// Build control on the page
		ganttChartControl.create(htmlDiv1);

		ganttGraphics = new jsGraphics(document.getElementById("ganttDiv"));
	});

	
	console.log('schedule:end');
	


}



window.onload = createChartControl('ganttDiv');
//* do porawy narazie prowizorka*/
function getProjectById(projects,id)
{
	if(id == 0)
		return projects.project0;
	if(id == 1)
		return projects.project1;
	if(id == 2)
		return projects.project2;
	if(id == 3)
		return projects.project3;
	if(id == 4)
		return projects.project4;
}


function findPos(obj) {
	var curleft = curtop = 0;

	if (obj.offsetParent) {

		do {
			if ((obj.style.left.replace(/\s/g, "") == "")) {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} else {
				curleft += parseInt(obj.style.left);
				curtop += parseInt(obj.style.top);

				break;
			}

		} while (obj = obj.offsetParent);
	}

	return [ curleft, curtop ];
}
